# -*- encoding=utf8 -*-
__author__ = "Simon"

from airtest.core.api import *
import sys

auto_setup(__file__,devices=["android://127.0.0.1:5037/127.0.0.1:7555"])
#auto_setup(__file__)
width,height = device().get_current_resolution()
#开始游戏
def start():
    #屏蔽暂时的下拉bug
    touch((width*0.01,height*0.5))
    sleep(1)
    touch((width*0.01,height*0.5))
    sleep(1)
    touch(Template(r"tpl1627657442392.png", record_pos=(0.341, -0.061), resolution=(1024, 576)))
    sleep(20)
    
    #进入游戏主界面
    while True:
        if not exists(Template(r"tpl1627658159996.png", record_pos=(-0.428, 0.244), resolution=(1024, 576))):
            sleep(5)
        else:
            touch((1,1))
            break
    sleep(20)
    
    #每日签到
    t = 0
    while True:
        if not exists(Template(r"tpl1627707520150.png", record_pos=(0.45, -0.244), resolution=(1024, 576))):
            t+=1
            if t>=2:
                print("签到超时")
                break
            sleep(5)
        else:
            touch(Template(r"tpl1627707520150.png", record_pos=(0.45, -0.244), resolution=(1024, 576)))
            break
    sleep(10)

    #跳过通知
    #width,height = device().get_current_resolution()
    while True:
        if not exists(Template(r"tpl1627721144200.png", record_pos=(0.002, -0.238), resolution=(1024, 576))):
            sleep(5)
        else:
            touch((width*0.955,height*0.033))
            break;
#行会点赞
def good():
    sleep(5)
    touch((width*0.72,height*0.8))
    sleep(10)
    
    #别人点赞给我
    if exists(Template(r"tpl1627740445778.png", record_pos=(-0.001, -0.128), resolution=(1024, 576))):
        touch(Template(r"tpl1627740482849.png", record_pos=(-0.003, 0.103), resolution=(1024, 576)))
    sleep(5)

    while True:
        if not exists(Template(r"tpl1627722311331.png", record_pos=(-0.254, 0.083), resolution=(1024, 576))):
            sleep(5)
        else:
            touch(Template(r"tpl1627722311331.png", record_pos=(-0.254, 0.083), resolution=(1024, 576)))
            break
    sleep(10)
    
    t = 0;
    while True:
        if not exists(Template(r"tpl1627723026273.png", rgb=True, record_pos=(0.381, -0.078), resolution=(1024, 576))): #rgb
            sleep(5)
            t+=1
            if t>=2:
                print("已点赞")
                break
        else:
            touch(Template(r"tpl1627723026273.png", record_pos=(0.381, -0.078), resolution=(1024, 576)))
            wait(Template(r"tpl1627723254198.png", record_pos=(-0.002, 0.103), resolution=(1024, 576)))
            touch(Template(r"tpl1627723254198.png", record_pos=(-0.002, 0.103), resolution=(1024, 576)))
            break
    sleep(5)
    touch((width*0.1,height*0.95))
#任务
def task():
    sleep(5)
    touch((width*0.87,height*0.8))
    sleep(5)
    t = 0
    while True:
        if not exists(Template(r"tpl1627723518632.png", rgb=True, record_pos=(0.378, 0.174), resolution=(1024, 576))): #rgb
            sleep(5)
            t+=1
            if t>=2:
                print("无可收取任务")
                break
        else:
            touch(Template(r"tpl1627723518632.png", record_pos=(0.378, 0.174), resolution=(1024, 576)))
            sleep(5)
            wait(Template(r"tpl1627723641115.png", record_pos=(-0.002, 0.215), resolution=(1024, 576)))
            touch(Template(r"tpl1627723641115.png", record_pos=(-0.002, 0.215), resolution=(1024, 576)))
            break
    sleep(5)
    touch((width*0.1,height*0.95))
#收礼物
def gift():
    sleep(5)
    touch((width*0.95,height*0.8))
    sleep(5)
    t = 0
    while True:
        if not exists(Template(r"tpl1627724144036.png", threshold=0.8, rgb=True, record_pos=(0.34, 0.214), resolution=(1024, 576))):#rgb
            sleep(5)
            t +=1
            if t>=2:
                print("无可收取礼物")
                break
        else:
            touch(Template(r"tpl1627724144036.png", record_pos=(0.34, 0.214), resolution=(1024, 576)))
            sleep(5)
            touch(Template(r"tpl1627724281576.png", record_pos=(0.111, 0.215), resolution=(1024, 576)))
            sleep(5)
            touch(Template(r"tpl1627724304844.png", record_pos=(-0.003, 0.215), resolution=(1024, 576)))
            break
    sleep(5)
    touch((1,1)) #退出礼物界面
#公会之家收体力
def sociaty():
    sleep(5)
    touch((width*0.65,height*0.95))
    sleep(10)
    if exists(Template(r"tpl1627724611154.png", record_pos=(0.001, -0.237), resolution=(1024, 576))):
        touch((width*0.49,height*0.87))
        sleep(5)
        
    t = 0
    while True:
        touch(Template(r"tpl1627724769852.png", rgb=False, record_pos=(0.437, 0.181), resolution=(1024, 576)))
        if not exists(Template(r"tpl1627741066309.png", record_pos=(-0.001, -0.237), resolution=(1024, 576))):#礼物盒判断
            sleep(5)
            t+=1
            if t>=2:
                print("无可收取")
                break
        else:
            touch(Template(r"tpl1627741138417.png", record_pos=(-0.004, 0.215), resolution=(1024, 576)))
            break
    sleep(5)
    touch((width*0.1,height*0.95))
#扭蛋
def gacha():
    sleep(5)
    touch((width*0.78,height*0.93))
    sleep(5)
    wait(Template(r"tpl1627725514972.png", record_pos=(0.429, -0.206), resolution=(1024, 576)))
    touch(Template(r"tpl1627725514972.png", record_pos=(0.429, -0.206), resolution=(1024, 576)))
    sleep(5)
    t = 0
    while True:
        if not exists(Template(r"tpl1627725643432.png", rgb=True, record_pos=(0.248, 0.105), resolution=(1024, 576))):#rgb
            sleep(5)
            t+=1
            if t>=2:
                print("没有扭蛋次数")
                break
        else:
            touch((width*0.75,height*0.65))
            wait(Template(r"tpl1627725753286.png", record_pos=(0.113, 0.103), resolution=(1024, 576)))
            touch(Template(r"tpl1627725753286.png", record_pos=(0.113, 0.103), resolution=(1024, 576)))
            sleep(10)
            touch(Template(r"tpl1627725815590.png", record_pos=(-0.001, 0.178), resolution=(1024, 576)))
            break
    sleep(5)
    touch((width*0.1,height*0.95))
#pjjc
def pjjc():
    sleep(5)
    touch((width*0.5,height*0.95))
    sleep(5)
    touch(Template(r"tpl1627726096027.png", record_pos=(0.36, 0.165), resolution=(1024, 576)))
    if exists(Template(r"tpl1627726134004.png", record_pos=(-0.002, -0.237), resolution=(1024, 576))):
        touch((1,1))
        sleep(5)
    if exists(Template(r"tpl1627728671108.png", rgb=True, record_pos=(-0.193, 0.071), resolution=(1024, 576))):#rgb
        touch(Template(r"tpl1627726223640.png", rgb=False, record_pos=(-0.194, 0.091), resolution=(1024, 576)))
        sleep(5)
        touch((width*0.5,height*0.68))
    sleep(5)
    touch((width*0.66,height*0.3))
    sleep(5)
    if exists(Template(r"tpl1627726513533.png", record_pos=(0.372, 0.19), resolution=(1024, 576))):
        touch(Template(r"tpl1627726513533.png", record_pos=(0.372, 0.19), resolution=(1024, 576)))
        if exists(Template(r"tpl1627726549212.png", record_pos=(0.372, 0.19), resolution=(1024, 576))):
            touch(Template(r"tpl1627726549212.png", record_pos=(0.372, 0.19), resolution=(1024, 576)))
            if exists(Template(r"tpl1627726591166.png", record_pos=(0.371, 0.189), resolution=(1024, 576))):
                touch(Template(r"tpl1627726591166.png", record_pos=(0.371, 0.189), resolution=(1024, 576)))
                sleep(120)
    t = 0
    while True:
        if exists(Template(r"tpl1627726752974.png", record_pos=(0.34, 0.229), resolution=(1024, 576))):
            touch(Template(r"tpl1627726752974.png", record_pos=(0.34, 0.229), resolution=(1024, 576)))
            break
        else:
            sleep(5)
            t+=1
            if t>=5:
                print("pjjc战斗出问题")
                break
    sleep(5)
    touch((width*0.1,height*0.95))
#jjc
def jjc():
    #width,height = device().get_current_resolution()
    sleep(5)
    touch((width*0.5,height*0.95))
    sleep(5)
    touch(Template(r"tpl1627727200643.png", record_pos=(0.105, 0.169), resolution=(1024, 576)))
    if exists(Template(r"tpl1627726134004.png", record_pos=(-0.002, -0.237), resolution=(1024, 576))):
        touch((1,1))
        sleep(5)
    if exists(Template(r"tpl1627728671108.png", rgb=True, record_pos=(-0.193, 0.071), resolution=(1024, 576))):#rgb
        touch(Template(r"tpl1627726223640.png", rgb=False, record_pos=(-0.194, 0.091), resolution=(1024, 576)))
        sleep(5)
        touch((width*0.5,height*0.68))
    sleep(5)
    touch((width*0.66,height*0.3))
    if exists(Template(r"tpl1627726591166.png", record_pos=(0.371, 0.189), resolution=(1024, 576))):
        touch(Template(r"tpl1627726591166.png", record_pos=(0.371, 0.189), resolution=(1024, 576)))
        sleep(60)
        
    t = 0
    while True:
        if exists(Template(r"tpl1627824267788.png", record_pos=(0.365, 0.229), resolution=(1024, 576))):
            touch(Template(r"tpl1627824267788.png", record_pos=(0.365, 0.229), resolution=(1024, 576)))
            break
        else:
            sleep(5)
            t+=1
            if t>=3:
                print("jjc战斗出问题")
                break
    sleep(5)
    touch((width*0.1,height*0.95))
#心碎
def heart():
    sleep(5)
    touch((width*0.5,height*0.95))
    sleep(5)
    touch(Template(r"tpl1627729389160.png", record_pos=(0.267, 0.043), resolution=(1024, 576)))
    sleep(5)
    
    touch((width*0.76,height*0.26))
    sleep(5)
    touch(Template(r"tpl1627729670912.png", record_pos=(0.416, 0.063), resolution=(1024, 576)),times=5)
    sleep(1)
    touch(Template(r"tpl1627729934240.png", record_pos=(0.26, 0.063), resolution=(1024, 576)))
    touch(Template(r"tpl1627729951091.png", record_pos=(0.112, 0.103), resolution=(1024, 576)))
    sleep(20)
    touch(Template(r"tpl1627730085194.png", record_pos=(-0.004, 0.214), resolution=(1024, 576)))
    sleep(5)
    if exists(Template(r"tpl1627730195507.png", record_pos=(-0.001, -0.129), resolution=(1024, 576))):
        touch(Template(r"tpl1627730225844.png", record_pos=(-0.115, 0.104), resolution=(1024, 576)))
        sleep(5)
        
    touch((width*0.025,height*0.46))
    sleep(5)
    
    
    touch(Template(r"tpl1627729670912.png", record_pos=(0.416, 0.063), resolution=(1024, 576)),times=5)
    sleep(1)
    touch(Template(r"tpl1627729934240.png", record_pos=(0.26, 0.063), resolution=(1024, 576)))
    touch(Template(r"tpl1627729951091.png", record_pos=(0.112, 0.103), resolution=(1024, 576)))
    sleep(20)
    touch(Template(r"tpl1627730085194.png", record_pos=(-0.004, 0.214), resolution=(1024, 576)))
    sleep(5)
    if exists(Template(r"tpl1627730195507.png", record_pos=(-0.001, -0.129), resolution=(1024, 576))):
        touch(Template(r"tpl1627730225844.png", record_pos=(-0.115, 0.104), resolution=(1024, 576)))
        sleep(5)
        
    touch((1,1))#退出刷图界面
    sleep(5)
    touch((width*0.1,height*0.95))
#探索
def explore():
    sleep(5)
    touch((width*0.5,height*0.95))
    touch(Template(r"tpl1627730828293.png", record_pos=(0.266, -0.101), resolution=(1024, 576)))
    sleep(5)
    
    #mana
    touch(Template(r"tpl1627730862798.png", record_pos=(0.339, 0.065), resolution=(1024, 576)))
    touch(Template(r"tpl1627730912308.png", target_pos=1, record_pos=(0.422, -0.013), resolution=(1024, 576)))
    sleep(5)
    for num in range(0,2):
        touch(Template(r"tpl1627729670912.png", record_pos=(0.416, 0.063), resolution=(1024, 576)))
        sleep(1)
    touch(Template(r"tpl1627729934240.png", record_pos=(0.26, 0.063), resolution=(1024, 576)))
    touch(Template(r"tpl1627729951091.png", record_pos=(0.112, 0.103), resolution=(1024, 576)))
    sleep(15)
    if exists(Template(r"tpl1627730195507.png", record_pos=(-0.001, -0.129), resolution=(1024, 576))):
        touch(Template(r"tpl1627730225844.png", record_pos=(-0.115, 0.104), resolution=(1024, 576)))
        sleep(5)
        
    touch(Template(r"tpl1627733669080.png", record_pos=(-0.003, 0.215), resolution=(1024, 576)))
    sleep(5)
    
    #exp
    touch(Template(r"tpl1627733889777.png", record_pos=(0.108, 0.065), resolution=(1024, 576)))
    touch(Template(r"tpl1627730912308.png", target_pos=1, record_pos=(0.422, -0.013), resolution=(1024, 576)))
    sleep(5)
    for num in range(0,2):
        touch(Template(r"tpl1627729670912.png", record_pos=(0.416, 0.063), resolution=(1024, 576)))
        sleep(1)
    touch(Template(r"tpl1627729934240.png", record_pos=(0.26, 0.063), resolution=(1024, 576)))
    touch(Template(r"tpl1627729951091.png", record_pos=(0.112, 0.103), resolution=(1024, 576)))
    sleep(15)
    if exists(Template(r"tpl1627730195507.png", record_pos=(-0.001, -0.129), resolution=(1024, 576))):
        touch(Template(r"tpl1627730225844.png", record_pos=(-0.115, 0.104), resolution=(1024, 576)))
        sleep(5)
        
    touch(Template(r"tpl1627733669080.png", record_pos=(-0.003, 0.215), resolution=(1024, 576)))
    sleep(5)
    touch((width*0.1,height*0.95))
#地下城
def tower():
    sleep(5)
    touch((width*0.5,height*0.95))
    sleep(5)
    touch(Template(r"tpl1627734620596.png", record_pos=(0.413, -0.101), resolution=(1024, 576)))
    sleep(5)
    touch((width*0.85,height*0.44))
    if exists(Template(r"tpl1627734693870.png", record_pos=(0.113, 0.104), resolution=(1024, 576))):
        touch(Template(r"tpl1627734693870.png", record_pos=(0.113, 0.104), resolution=(1024, 576)))
    sleep(10)
    
    touch(Template(r"tpl1627734742062.png", record_pos=(-0.003, 0.053), resolution=(1024, 576)))
    touch(Template(r"tpl1627734781730.png", record_pos=(0.375, 0.191), resolution=(1024, 576)))
    sleep(5)
    touch(Template(r"tpl1627734903896.png", record_pos=(0.396, -0.188), resolution=(1024, 576)))
    wait(Template(r"tpl1627735134231.png", record_pos=(0.002, -0.237), resolution=(1024, 576)))
    touch(Template(r"tpl1627735268337.png", target_pos=2, record_pos=(0.322, 0.023), resolution=(1024, 576)))
    sleep(5)
    touch(Template(r"tpl1627736903500.png", record_pos=(0.371, 0.189), resolution=(1024, 576)))
    sleep(30)
    touch(Template(r"tpl1627737536659.png", record_pos=(0.365, 0.229), resolution=(1024, 576)))
    sleep(5)
    touch(Template(r"tpl1627737558255.png", record_pos=(-0.003, 0.215), resolution=(1024, 576)))
    sleep(10)
    
    touch(Template(r"tpl1627737583336.png", record_pos=(0.184, 0.081), resolution=(1024, 576)))
    touch(Template(r"tpl1627734781730.png", record_pos=(0.375, 0.191), resolution=(1024, 576)))
    sleep(5)
    touch(Template(r"tpl1627736903500.png", record_pos=(0.371, 0.189), resolution=(1024, 576)))
    sleep(30)
    touch(Template(r"tpl1627737536659.png", record_pos=(0.365, 0.229), resolution=(1024, 576)))
    sleep(5)
    touch(Template(r"tpl1627737558255.png", record_pos=(-0.003, 0.215), resolution=(1024, 576)))
    sleep(10)
    
    touch(Template(r"tpl1627737713137.png", record_pos=(-0.005, 0.098), resolution=(1024, 576)))
    touch(Template(r"tpl1627734781730.png", record_pos=(0.375, 0.191), resolution=(1024, 576)))
    sleep(5)
    touch(Template(r"tpl1627736903500.png", record_pos=(0.371, 0.189), resolution=(1024, 576)))
    sleep(30)
    touch(Template(r"tpl1627737536659.png", record_pos=(0.365, 0.229), resolution=(1024, 576)))
    sleep(5)
    touch(Template(r"tpl1627737558255.png", record_pos=(-0.003, 0.215), resolution=(1024, 576)))
    sleep(10)
    
    touch(Template(r"tpl1627737839299.png", record_pos=(-0.078, 0.063), resolution=(1024, 576)))
    touch(Template(r"tpl1627734781730.png", record_pos=(0.375, 0.191), resolution=(1024, 576)))
    sleep(5)
    touch(Template(r"tpl1627736903500.png", record_pos=(0.371, 0.189), resolution=(1024, 576)))
    sleep(30)
    touch(Template(r"tpl1627737536659.png", record_pos=(0.365, 0.229), resolution=(1024, 576)))
    sleep(5)
    touch(Template(r"tpl1627737558255.png", record_pos=(-0.003, 0.215), resolution=(1024, 576)))
    sleep(10)

    #tudo:第五层
    
    touch((width*0.1,height*0.95))

#刷图
def ttk():
    sleep(5)
    touch((width*0.5,height*0.95))
    sleep(5)
    touch(Template(r"tpl1627882784613.png", record_pos=(0.085, 0.029), resolution=(1024, 576)))
    sleep(5)
    #搜索地图
    t = 0 #1代表找到地图
    c = 0 #查找地图的次数
    v = 0 #0代表向右查询
    while True:
        if exists(Template(r"tpl1627890794120.png", record_pos=(-0.377, -0.213), resolution=(1024, 576))): #要找的地图
            print("找到指定地图")
            t = 1
            break
        if exists(Template(r"tpl1627891245970.png", threshold=0.45, record_pos=(-0.338, -0.213), resolution=(1024, 576))):
            print("到右边界")
            v = 1
        if exists(Template(r"tpl1627891093421.png", record_pos=(-0.393, -0.213), resolution=(1024, 576))):
            print("到左边界")
            print("已完成对所有地图查询，未找到")
            break
            sys.exit()
        if v == 0:
            touch((width*0.97,height*0.47)) #向右查询
        else:
            touch((width*0.03,height*0.45)) #向左查询
            
        c+=1
        if c>=50:
            print("查询地图失败")
            break
            sys.exit()
            
    #搜索关卡
    c = 0 #查找关卡的次数
    t = 1 #滑动方向判断
    while True:
        if not exists(Template(r"tpl1627892699763.png", threshold=0.71, record_pos=(0.335, 0.016), resolution=(1024, 576))):
            if t == 1:
                swipe((width*0.99,height*0.4),(width*0.01,height*0.4)) #向左划
                t = 0
            else:
                swipe((width*0.01,height*0.4),(width*0.99,height*0.4)) #向右划
                t = 1
        else:
            L = (wait(Template(r"tpl1627892699763.png", threshold=0.71, record_pos=(0.335, 0.016), resolution=(1024, 576)))) #用列表接收坐标
            m = (L[0],L[1]-20) #坐标偏移
            touch(m) #点入指定关卡
            break
            
        c+=1
        if c>=3:
            print("查询关卡失败")
            break
            sys.exit()
            
    #战斗
    
    #刷图次数
    sleep(5)
    for num in range(0,1):
        touch((width*0.91,height*0.61))
    #开始战斗
    touch((width*0.75,height*0.62))
    touch(Template(r"tpl1627887184673.png", record_pos=(0.113, 0.103), resolution=(1024, 576)))
    sleep(10)
    n = 0
    while True:
        if not exists(Template(r"tpl1627887250714.png", record_pos=(-0.003, 0.214), resolution=(1024, 576))):
            sleep(10)
            n+=1
            if n>=4:
                print("扫荡结果等待失败")
                break
        else:
            #跳过限定商店
            sleep(5)
            if exists(Template(r"tpl1627730195507.png", record_pos=(-0.001, -0.129), resolution=(1024, 576))):
                touch(Template(r"tpl1627730225844.png", record_pos=(-0.115, 0.104), resolution=(1024, 576)))
                sleep(5)
            touch(Template(r"tpl1627887250714.png", record_pos=(-0.003, 0.214), resolution=(1024, 576)))
            print("刷图完成")
            sleep(5)
            touch((1,1))
            sleep(5)
            touch((width*0.1,height*0.95))
            break

        
          



    

    
start()
task()
gift()
sociaty()
good()
gacha()
pjjc()
jjc()
heart()
explore()
tower()
#ttk()


